package com.fenghuajueli.libbasecoreui.ui;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.LinearLayout;

import com.blankj.utilcode.util.LogUtils;
import com.fenghuajueli.libbasecoreui.R;
import com.fenghuajueli.libbasecoreui.baseswitch.SwitchKeyUtils;
import com.fenghuajueli.libbasecoreui.constants.AppConfigInfo;
import com.fenghuajueli.libbasecoreui.constants.ConfigConstants;
import com.fenghuajueli.libbasecoreui.constants.PrivacyConstantsUtils;
import com.fenghuajueli.libbasecoreui.constants.SwitchNetConstants;
import com.fenghuajueli.libbasecoreui.mvp.BaseActivity;
import com.fenghuajueli.libbasecoreui.utils.MmkvUtils;
import com.fenghuajueli.libbasecoreui.widget.MyActionBar;
import com.fenghuajueli.libbasecoreui.widget.ProgressWebview;
import com.fenghuajueli.libbasecoreui.widget.RichTextView;

public class PrivacyWebActivity extends BaseActivity {

    private static final String TYPE = "type";

    private ProgressWebview webView;

    private RichTextView richTextView;
    private MyActionBar myActionBar;
    private String currentType = "";
    private static final String privacyUrl = AppConfigInfo.PRIVACY_URL + "?";
    private static final String userProtocolUrl = "https://www.bestkids.net/cut-video/app/generalPolicy/userAgreement3.html?";
    private static final String vipUrl = "https://www.bestkids.net/cut-video/app/generalPolicy/purchaseInstructions.html?";
    private static final String childrenPrivacyUrl = "https://ijj-bestkids-web.oss-cn-shanghai.aliyuncs.com/children_private_policy.html?";

    /**
     * 订阅协议地址
     */
    private static final String subscribeAgreementUrl = "http://www.bestkids.net/cut-video/app/generalPolicy/autoDeductionpolicy.html?";

    /**
     * 跳转协议
     *
     * @param context
     * @param type
     */
    public static void start(Context context, PrivacyConstantsUtils.ProtocolType type) {
        Intent intent = new Intent(context, PrivacyWebActivity.class);
        intent.putExtra(TYPE, type.name());
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_privacy_web);
        richTextView = findViewById(R.id.richText);
        myActionBar = findViewById(R.id.myActionBar);
        currentType = getIntent().getStringExtra(TYPE);
        String showPrivacyUrl = privacyUrl;
        String contentUrl = "";

        if (PrivacyConstantsUtils.ProtocolType.PRIVACY.name().equals(currentType)) {
            String par = String.format("appName=%s&corporateName=%s&bgColor=%s&fontColor=%s",
                    PrivacyConstantsUtils.appName,
                    PrivacyConstantsUtils.companyName,
                    PrivacyConstantsUtils.bgColor,
                    PrivacyConstantsUtils.textColor);
            contentUrl = showPrivacyUrl + par;
            myActionBar.setTitle("隐私政策");
        } else if (PrivacyConstantsUtils.ProtocolType.USER_PROTOCOL.name().equals(currentType)) {
            String par = String.format("appName=%s&corporateName=%s&bgColor=%s&fontColor=%s&app_id=%s&channel=%s",
                    PrivacyConstantsUtils.appName,
                    PrivacyConstantsUtils.companyName,
                    PrivacyConstantsUtils.bgColor,
                    PrivacyConstantsUtils.textColor,
                    PrivacyConstantsUtils.applicationId,
                    PrivacyConstantsUtils.channelId);
//            if (!MmkvUtils.get(ConfigConstants.MMKVKEY.privacyDialogIsShow.name(), false)) {
                String apiUrl = AppConfigInfo.APP_DEBUG ? "api-test/an-user-agreement?" : "api/an-user-agreement?";
                String baseUrl = SwitchNetConstants.BASE_SWITCH_KEY_URL + apiUrl;
                contentUrl = baseUrl + par;
//            } else {
//                contentUrl = userProtocolUrl + par;
//            }
            myActionBar.setTitle("用户协议");
        } else if (PrivacyConstantsUtils.ProtocolType.BUY_PROTOCOL.name().equals(currentType)) {
            String par = String.format("appName=%s&corporateName=%s&bgColor=%s&fontColor=%s&vipContent=%s&vipDescribe=%s&qq=%s",
                    PrivacyConstantsUtils.appName,
                    PrivacyConstantsUtils.companyName,
                    PrivacyConstantsUtils.bgColor,
                    PrivacyConstantsUtils.textColor,
                    PrivacyConstantsUtils.vipContent,
                    PrivacyConstantsUtils.vipDesc,
                    PrivacyConstantsUtils.qq
            );
            contentUrl = vipUrl + par;
            myActionBar.setTitle("购买须知");
        } else if (PrivacyConstantsUtils.ProtocolType.SUBSCRIBE_PROTOCOL.name().equals(currentType)) {
            if (TextUtils.isEmpty(AppConfigInfo.SUBSCRIBE_AGREEMENT_URL)) {
                String par = String.format("appName=%s&corporateName=%s",
                        PrivacyConstantsUtils.appName,
                        PrivacyConstantsUtils.companyName);
                contentUrl = subscribeAgreementUrl + par;
            } else {
                contentUrl = AppConfigInfo.SUBSCRIBE_AGREEMENT_URL;
            }
            myActionBar.setTitle("订阅协议");
        } else if (PrivacyConstantsUtils.ProtocolType.CHILDREN_PRIVACY.name().equals(currentType)) {
            String par = String.format("kf_url=%s&email_data=%s",
                    CustomerServiceActivity.getUrl(),
                    SwitchKeyUtils.getCompanyEmail());
            contentUrl = childrenPrivacyUrl + par;
            myActionBar.setTitle("儿童隐私声明");
        }

        LinearLayout containerView = findViewById(R.id.richTextSV);
        // 还没同意隐私政策，用rich text显示
        if (!MmkvUtils.get(ConfigConstants.MMKVKEY.privacyDialogIsShow.name(), false)) {
            richTextView.loadUrl(contentUrl);
            richTextView.setTextColor(Color.parseColor("#" + PrivacyConstantsUtils.textColor));
            richTextView.setBackgroundColor(Color.parseColor("#" + PrivacyConstantsUtils.bgColor));
        } else {
            webView = new ProgressWebview(this);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            webView.setLayoutParams(params);
            containerView.removeAllViews();
            containerView.addView(webView);
            String privacyData = MmkvUtils.get("WEB_PRIVACY_DATA", "");
            if (!TextUtils.isEmpty(privacyData)) {
                webView.setData(privacyData);
            }
            webView.loadUrl(contentUrl);
        }
    }

    @Override
    public void onBackPressed() {
        if (webView != null && webView.canGoBack()) {
            webView.goBack();
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (webView != null) {
            webView.destroy();
            webView = null;
        }
    }
}